<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>智能停车场管理系统 - 添加停车场</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            margin: 0;
            background-image: url('../images/停车场图片1.png');
            background-size: cover;
            background-position: center;
            background-repeat: no-repeat;
        }
    .popup {
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background-color: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
            width: 400px;
            text-align: center;
        }
    .popup h2 {
            margin-bottom: 20px;
            color: #00008B;
            font-weight: bold;
        }
    .form-group {
            margin-bottom: 15px;
        }
    .form-group label {
            display: block;
            margin-bottom: 5px;
            color: #555;
        }
    .form-group input {
            width: 100%;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }
    .form-group button {
            width: 100%;
            padding: 10px;
            background-color: #007BFF;
            color: #fff;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }
    .form-group button:hover {
            background-color: #0056b3;
        }
    .message {
            text-align: center;
            margin-top: 15px;
            color: green;
        }
    .return-button {
            margin-top: 15px;
            padding: 8px 16px;
            background-color: #007BFF;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }
    .return-button:hover {
            background-color: #0056b3;
        }
    .error-message {
            color: red;
            font-size: 14px;
            margin-top: 5px;
            display: none;
        }
    </style>
</head>
<body>
    <div class="popup">
        <h2>添加停车场</h2>
        <form id="addParkingForm">
            <div class="form-group">
                <label for="parkName">停车场名称</label>
                <input type="text" id="parkName" v-model="parkNameValue" name="parkName" @blur="checkParkName" minlength="1" maxlength="50" required pattern=".{1,50}" oninput="validateInput(this)">
                <br />
                <label :style="{color:parkNameTipsColor}">{{parkNameTips}}</label>
            </div>
            <div class="form-group">
                <label for="parkLocation">停车场地址</label>
                <input type="text" id="parkLocation" v-model="parkLocationValue" name="parkLocation" @blur="checkParkLocation" minlength="1" required pattern=".{1,}" oninput="validateInput(this)">
                <br />
                <label :style="{color:parkLocationTipsColor}">{{parkLocationTips}}</label>
            </div>
            <div class="form-group">
                <label for="parkSeats">停车场车位数量</label>
                <input type="number" id="parkSeats" v-model="parkSeatsValue" name="parkSeats" @blur="checkParkSeats" min="1" required oninput="validateInput(this)">
                <br />
                <label :style="{color:parkSeatsTipsColor}">{{parkSeatsTips}}</label>
            </div>
            <div class="form-group">
                <button type="button" @click="addParking" class="add-parking-btn">添加停车场</button>
            </div>
        </form>
        <div class="message" id="message"></div>
        <button class="return-button" onclick="window.location.href = 'parking_info.html'">返回</button>
    </div>
    <script src="layui/layui.js" type="text/javascript"></script>
    <script src="js/vue.js" type="text/javascript"></script>
    <script src="js/jquery-3.6.0.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        let vm = new Vue({
            el: "#addParkingForm",
            data: {
                parkNameValue: "",
                parkLocationValue: "",
                parkSeatsValue: "",
                parkNameTips: "",
                parkNameTipsColor: "",
                parkLocationTips: "",
                parkLocationTipsColor: "",
                parkSeatsTips: "",
                parkSeatsTipsColor: ""
            },
            methods: {
                checkParkName() {
                    let parkName = this.parkNameValue;
                    if (parkName.length < 1 || parkName.length > 50) {
                        this.parkNameTips = "停车场名称长度需在1到50个字符之间";
                        this.parkNameTipsColor = "red";
                    } else {
                        this.parkNameTips = "";
                        this.parkNameTipsColor = "";
                    }
                },
                checkParkLocation() {
                    let parkLocation = this.parkLocationValue;
                    if (parkLocation.length < 1) {
                        this.parkLocationTips = "停车场地址不能为空";
                        this.parkLocationTipsColor = "red";
                    } else {
                        this.parkLocationTips = "";
                        this.parkLocationTipsColor = "";
                    }
                },
                checkParkSeats() {
                    let parkSeats = this.parkSeatsValue;
                    if (isNaN(parkSeats) || parkSeats < 1) {
                        this.parkSeatsTips = "停车场车位数量必须为正整数";
                        this.parkSeatsTipsColor = "red";
                    } else {
                        this.parkSeatsTips = "";
                        this.parkSeatsTipsColor = "";
                    }
                },
                addParking() {
                    let url = "http://localhost:8080/parking/insert";
                    let parkingInfo = {
                        parkName: this.parkNameValue,
                        parkLocation: this.parkLocationValue,
                        parkSeats: parseInt(this.parkSeatsValue)
                    };
                    $.post(url, parkingInfo, function (res) {
                        if (res.code === 200) {
                            document.getElementById('message').textContent = '添加成功';
                            document.getElementById('errorMessage').style.display = 'none';
                        } else {
                            document.getElementById('message').textContent = res.msg;
                            document.getElementById('errorMessage').style.display = 'block';
                        }
                    }, "JSON");
                }
            }
        });
        function validateInput(input) {
            var errorElement = document.getElementById(input.id + 'Error');
            errorElement.textContent = '';
        }
    </script>
</body>
</html>